﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ryder_US_Website.BUS;
using System.Xml.Linq;
using Ryder_US_Website.Utils;
namespace Ryder_US_Website.Presentation.Admin
{
    public partial class User_Update : System.Web.UI.Page
    {
        public string id;
        RUS_User user = new RUS_User();
        RUS_User curUser = new RUS_User();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["RUS_User"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            curUser = (RUS_User)Session["RUS_User"];
            if(curUser.User_role!=0)
            {
                Response.Redirect("Home.aspx");
            }
            id = Request.QueryString["id"];
            int temp = Int16.Parse(id);
            user = UserBUS.GetUserByID(temp);
            if (!IsPostBack)
            {
                rbQuanTri.Checked = (user.User_role == 0);
                rbNhanVien.Checked = !rbQuanTri.Checked;
                txtTenDangNhap.Text = user.User_username;
                //chbMatKhau.Checked = false;
            }
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            RUS_User kq = new RUS_User();
            user.User_role = (rbNhanVien.Checked == true) ? 1 : 0;
            user.User_username = txtTenDangNhap.Text;
            //chbMatKhau không được check
            try
            {
                if (chbMatKhau.Checked == false)
                {
                    kq = UserBUS.Update(user.User_username, user.User_role, user.User_ID);
                }
                else
                {
                    //if (txtMatKhau.Text == "")
                    //{
                    //    Response.Write(UtilHelpers.ShowMessage("Mời bạn nhập mật khẩu"));
                    //    chbMatKhau.Checked = false;
                    //}
                    //else
                    //{
                        user.User_password = txtMatKhau.Text;
                        kq = UserBUS.Update(user.User_username, user.User_password, user.User_role, user.User_ID);
                    //}
                }
            }
            catch(Exception ex)
            {
                Response.Write(UtilHelpers.ShowMessage(ex.Message));
            }
            
            if (user.User_ID.Equals(kq.User_ID))
            {
                Response.Redirect("User_List.aspx");
            }
            else
            {
                Response.Write(UtilHelpers.ShowMessage("Cập nhật không thành công"));
            }
           
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("User_List.aspx");
        }
    }
}
